
*********************************************************************************
** PRELIMINARIES
*********************************************************************************

********************************************************************
** WAVE 2 POST-PLANTING DATA
********************************************************************

use "$dir/rawdata/LSMS Nigeria/2012/Post Planting Wave 2/Household/sect1_plantingw2.dta", clear // ROSTER
count
local samplesize=`r(N)'
merge 1:1 hhid indiv using "$dir/rawdata/LSMS Nigeria/2012/Post Planting Wave 2/Household/sect2_plantingw2.dta", gen(merge_sec2) assert(master matched) // EDUCATION
merge 1:1 hhid indiv using "$dir/rawdata/LSMS Nigeria/2012/Post Planting Wave 2/Household/sect3a_plantingw2.dta", gen(merge_sec3a) assert(master matched) // LABOR A
merge 1:1 hhid indiv using "$dir/rawdata/LSMS Nigeria/2012/Post Planting Wave 2/Household/sect3b_plantingw2.dta", gen(merge_sec3b) assert(master matched) // LABOR B
merge 1:1 hhid indiv using "$dir/rawdata/LSMS Nigeria/2012/Post Planting Wave 2/Household/sect4b_plantingw2.dta", gen(merge_sec4b) assert(master matched) // FINANCIAL CAPABILITY

** Still in HH
gen w2p_inhh=s1q4!=2

** Household size
gen temp=1 if w2p_inhh==1
bys hhid: egen w2p_hh_size=total(temp)
drop temp

** Number of children 12 or younger
gen temp=1 if s1q6<=12 & w2p_inhh==1
bys hhid: egen w2p_hh_under12=total(temp)
drop temp

** Number of youth 18 or younger
gen temp=1 if s1q6<=18 & w2p_inhh==1
bys hhid: egen w2p_hh_under18=total(temp)
drop temp

** Number of adults 60 or older
gen temp=1 if s1q6>=60 & !missing(s1q6) & s1q6!=999 & w2p_inhh==1
bys hhid: egen w2p_hh_over60=total(temp)
drop temp

** Adults 12 to 60
gen temp=1 if s1q6>12 & s1q6<60 & !missing(s1q4) & s1q6!=999 & w2p_inhh==1
bys hhid: egen w2p_hh_12to60=total(temp)
drop temp

** Marital status
gen w2p_married=(s1q8==1 | s1q8==2) if !missing(s1q8)
gen w2p_married_monog=s1q8==1 if !missing(s1q8)
gen w2p_married_polyg=s1q8==2 if !missing(s1q8)

** Female
gen w2p_female=(s1q2==2) if !missing(s1q2)

** Age
gen w2p_age=s1q6
replace w2p_age=. if w2p_age==999

** Age of spouse
sum indiv 
forvalues x=1/`r(max)' {
	gen age_person`x'_temp=s1q6 if indiv==`x' & !missing(s1q6)
	bys hhid: egen age_person`x'=max(age_person`x'_temp)
	drop age_person`x'_temp
}

** Average age of others in the household
sum indiv 
forvalues x=1/`r(max)' {
	replace age_person`x'=. if indiv==`x'
}
egen w2p_hh_avgage=rowmean(age_person*)

** Spouse age
sum indiv
forvalues x=1/`r(max)' {
	replace age_person`x'=. if s1q13!=`x'
}
egen w2p_spouse_age=rowmean(age_person*)

** Age gap
gen w2p_spouse_agegap=w2p_age-w2p_spouse_age

** Birthday
gen w2p_birthday=mdy(s1q7_month,s1q7_day,s1q7_year)
format w2p_birthday %td

** Birth year
gen w2p_birthyear=s1q7_year
recode w2p_birthyear 9999=. 9998=.
replace w2p_birthyear=2012-w2p_age if missing(w2p_birthyear) // FIXME: Not all (but most) are actually 2012

** Highest education of individual
gen w2p_educ=s2q8
replace w2p_educ=0 if s2q5==2 // Never attended school
label values w2p_educ S2Q8
replace w2p_educ=. if s2q8==98

** Still in school
gen w2p_inschool=(s2q10==1) if !missing(s2q10)

** Literate
gen w2p_literate=(s2q4==1) if !missing(s2q4)

** Worked for a non-HH member
gen w2p_work_outsideHH=s3aq4==1 if !missing(s3aq4)

** Worked in agriculture for a HH member
gen w2p_work_agHH=s3aq5==1 if !missing(s3aq5)

** Worked in HH business
gen w2p_work_bizHH=s3aq6==1 if !missing(s3aq6)

** Any work in last 7 days (agriculture, for non-HH member, own account/business)
gen w2p_worked_last7days=0
replace w2p_worked_last7days=1 if s3aq4==1
replace w2p_worked_last7days=1 if s3aq5==1 
replace w2p_worked_last7days=1 if s3aq6==1
replace w2p_worked_last7days=. if missing(s3aq4) & missing(s3aq5) & missing(s3aq6)

** Hours worked in last 7 days
egen w2p_work_hrs=rowtotal(s3aq18 s3aq31)
replace w2p_work_hrs=. if missing(w2p_worked_last7days)

** Works in agriculture
gen w2p_work_agri=(w2p_work_agHH==1 | s3aq14==1) if (!missing(w2p_work_agHH) | !missing(s3aq27))

** Industry of occupation
gen w2p_occ=s3aq14
label values w2p_occ S3AQ14

** Highest education of biological father
	gen w2p_father_educ=s1q22
	label values w2p_father_educ S1Q22
	replace w2p_father_educ=. if w2p_father_educ==98

** Industry of occupation of biological father
	gen w2p_father_occ=s1q23
	label values w2p_father_occ S1Q23

** Highest education of biological mother
	gen w2p_mother_educ=s1q27
	label values w2p_mother_educ S1Q27
	replace w2p_mother_educ=. if w2p_mother_educ==98

** Industry of occupation of biological mother
	gen w2p_mother_occ=s1q28
	label values w2p_mother_occ S1Q28

** ID of father and mother (for linking)
	gen w2p_father_id=s1q20 
	gen w2p_mother_id=s1q25

** GEOGRAPHIC INFORMATION
	gen w2p_geo_urban=(sector==1) if !missing(sector)
	gen w2p_geo_state=state
	gen w2p_geo_lga=lga
	replace w2p_geo_lga=s1q32b if !missing(s1q32b) // LGA code for where the HH member moved
	label var w2p_geo_urban "Urban residence"
	label var w2p_geo_state "State of residence"
	label var w2p_geo_lga "LGA of residence"

** Contributes to the budget
	gen w2p_contributes_budget=s4bq1==1 if !missing(s4bq1) // asked of individuals 15 years or older

** Participates in spending decisions
	gen w2p_spending_decisions=s4bq2==1 if !missing(s4bq2)

** Mainly responsible for own spending
	gen w2p_mainresp_spending=s4bq3==1 if !missing(s4bq3)

** Partly responsible for own spending
	gen w2p_partresp_spending=s4bq3==2 if !missing(s4bq3)

** Responsible for managing money day-to-day in HH
	gen w2p_resp_money_selfonly=s4bq19==1 if !missing(s4bq19)
	replace w2p_resp_money_selfonly=0 if w2p_mainresp_spending==0 & w2p_spending_decisions==0 & missing(w2p_resp_money_selfonly) // follows this skip pattern
	
	gen w2p_resp_money_selfplusoth=(s4bq19==1 | s4bq19==3 | s4bq19==4) if !missing(s4bq19)
	replace w2p_resp_money_selfplusoth=0 if w2p_mainresp_spending==0 & w2p_spending_decisions==0 & missing(w2p_resp_money_selfplusoth) // follows this skip pattern

	gen w2p_resp_money_others=(s4bq19==2 | s4bq19==5 | s4bq19==6) if !missing(s4bq19)
	replace w2p_resp_money_others=1 if w2p_mainresp_spending==0 & w2p_spending_decisions==0 & missing(w2p_resp_money_others) // follows this skip pattern

** Worried about unexpected expenses
	gen w2p_bitworr_futureexp=s4bq25==2 if !missing(s4bq25)
	gen w2p_veryworr_futureexp=s4bq25==3 if !missing(s4bq25)
	
** HH head is worried about unexpected expenses
	sort hhid indiv
	bys hhid: egen temp=first(s4bq25)
	bys hhid: egen temp2=max(temp)
	gen w2p_hh_bitworr_futureexp=temp2==2 if !missing(temp2)
	gen w2p_hh_veryworr_futureexp=temp2==3 if !missing(temp2)
	drop temp*
	
** HH size 
	merge m:1 hhid using "$dir/rawdata/LSMS Nigeria/2012/cons_agg_wave2_visit1.dta", assert(master matched) nogen

	gen w2p_urban_fromcons=(rururb==1) if !missing(rururb)
	order w2p_urban_fromcons, after(rururb)
	label var w2p_urban_fromcons "Urban residence"

	ren hhsize w2p_hhsize_fromcons
	ren totcons w2p_conspc


********************************************************************
** LABEL AND SAVE
********************************************************************

keep hhid indiv w2p*
order hhid indiv w2p_geo*
save "$dir/data/NGA_w2p_individual.dta", replace
